# -*- coding: utf-8 -*-
"""
Created on Thu Sep  8 16:05:53 2022

@author: 123
"""

import pandas as pd
import numpy as np

#导入第一步结果数据集
#df=pd.read_csv(r'D:\kaggle论文准备\ruc_master_paper\code\01_step_result.csv')

#切片，分开 特征x 和 目标y

x, y = new_df.iloc[:,1:], new_df.iloc[:,0]

#数据过抽样处理

from imblearn.over_sampling import RandomOverSampler

ros_model = RandomOverSampler(random_state=0)
X_resampled, y_resampled = ros_model.fit_resample(x, y)


from collections import Counter
sorted(Counter(y_resampled).items())


#异常值处理 
x_train=np.array(X_resampled)
x_test=np.array(X_resampled)



from sklearn.covariance import EmpiricalCovariance, MinCovDet
from sklearn.covariance import EllipticEnvelope

cov_ros_model = EllipticEnvelope(random_state=0).fit(x_train)
x_test_predict_np=cov_ros_model.predict(x_test)


x_test_predict_pd=pd.DataFrame(x_test_predict_np)

#列重命名
p_col=['is_outlier']
x_test_predict_pd.columns=p_col

#is_outlier 字段和new_df 合并 
# df3 = pd.concat([df, df2], join="inner", axis=1)

X_resampled_df_outlier=pd.concat([X_resampled, x_test_predict_pd], join="inner", axis=1)

y_resampled_df_outlier=pd.concat([y_resampled, x_test_predict_pd], join="inner", axis=1)


#剔除异常值

#X_resampled_df_outlier1=X_resampled_df_outlier.query('is_outlier=="1"')

X_resampled_df_outlier1=X_resampled_df_outlier.query('is_outlier==1')
#y_resampled_df_outlier1=(y_resampled_df_outlier.query('is_outlier=="1"'))['Risk_Flag']

y_resampled_df_outlier1=(y_resampled_df_outlier.query('is_outlier==1'))['Risk_Flag']



